package com.scisoft.scan.control;

import java.util.ArrayList;
import java.util.List;
import java.util.prefs.NodeChangeEvent;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scisoft.scan.dy.R;

/**
 * 通用适配器Adapter写法
 *
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    //属性
    protected Context mContext; //上下文
    protected  List<T> mList; //数据源
    //左滑菜单的属性
    private List<String> list = new ArrayList<>();
    private int itemLayoutId;//列表子项的layoutid
    private int itemLayoutHideId;//列表子项的隐藏layoutid
    private int itemLayoutShowId;//列表子项的显示layoutid
    private int horizontalScrollViewId;//列表子项的布局id
    private int itemLayoutFullWidth;//列表子项的全长度

    //上拉下拉的属性
    public static final int PULLUP_LOAD_MORE = 0;//上拉状态0：上拉加载更多
    public static final int LOADING_MORE = 1;//上拉状态1：正在加载中
    public static final int LOADING_END = 2;//上拉状态1：已经到底了
    private static final int TYPE_FOOTER = 0;//带Footer的Item
    private static final int TYPE_NORMAL = 1;//不带Footer的Item
    private int load_more_status = 0; //上拉加载状态，默认为状态0-上拉加载更多
    private boolean multiPage = true;//是否分页 默认分页显示

    //构造函数
    public CommonAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }

    //根据Item位置返回viewType，供onCreateViewHolder方法内获取不同的Holder
    @Override
    public int getItemViewType(int position) {
        if (multiPage) {
            if (position == getItemCount() - 1) {
                return TYPE_FOOTER;
            } else {
                return TYPE_NORMAL;
            }
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public int getItemCount() {
        if (multiPage) {
            //返回item个数，由于footerview的存在，所以item个数要+1
            return mList.size() + 1;
        } else {
            return mList.size();
        }
    }
    //创建ViewHolder，如果是header或者footer直接返回响应holder即可
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_NORMAL) {
            return CommonViewHolder.getInstance(mContext, itemLayoutId, parent, horizontalScrollViewId, itemLayoutShowId, itemLayoutHideId);
        } else {
            return new FooterHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_footer, parent, false));
        }
    }

    //填充视图
    @Override
    @SuppressLint("ClickableViewAccessibility")
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        //普通Item，直接设置
        if (holder instanceof CommonViewHolder) {
            //填充数据
            if (mList.size() > 0)
                convert((CommonViewHolder) holder, mList.get(position), position);

            if (mOnItemClickListener != null) {
                ((CommonViewHolder) holder).mShowView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(((CommonViewHolder) holder).itemView, position);
                    }
                });
            }
            //设置监听
            if (mOnItemLongClickListener != null) {
                ((CommonViewHolder) holder).mShowView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mOnItemLongClickListener.onItemLongClick(((CommonViewHolder) holder).itemView, position);
                        return true;// 返回true 表示消耗了事件 事件不会继续传递
                    }
                });
            }


            //侧滑的菜单处理逻辑
            //list用来记录itemView是否滑开，避免recyclerview复用出现bug
            if (list.contains(position + "")) {
                holder.itemView.scrollTo(((CommonViewHolder) holder).mHideView.getWidth(), 0);
            } else {
                holder.itemView.scrollTo(0, 0);
            }
            //将item要展示的内容宽度设置为屏幕宽度，删除按钮则会隐藏在屏幕右侧
            ViewGroup.LayoutParams layoutParams = ((CommonViewHolder) holder).mShowView.getLayoutParams();
            layoutParams.width = itemLayoutFullWidth;
            //监听屏幕的触摸事件实现侧滑按钮的显示
            ((CommonViewHolder) holder).horizontalScrollView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_UP:
                            //获取滑动的距离
                            int scrollX = ((CommonViewHolder) holder).horizontalScrollView.getScrollX();
                            //获取右侧删除按钮的宽度
                            int itemLayoutHideWidth = ((CommonViewHolder) holder).mHideView.getWidth();
                            //如果滑动距离超过了删除按钮宽度的1/2，则将删除按钮滑出，否则隐藏
                            if (scrollX >= itemLayoutHideWidth / 2) {
                                holder.itemView.scrollTo(itemLayoutHideWidth, 0);
                                //滑动的item记录下来
                                list.add(position + "");
                            } else {
                                holder.itemView.scrollTo(0, 0);
                                if (list.contains(position + "")) {
                                    //记录中移除
                                    list.remove(position + "");
                                }
                            }
                            break;
                        case MotionEvent.ACTION_MOVE:
                            break;
                    }
                    //注意此处返回false
                    return false;
                }
            });
        } else {
            //footerview，需要判断上拉状态
            switch (load_more_status) {
                //状态：上拉加载更多
                case PULLUP_LOAD_MORE:
                    ((FooterHolder) holder).mView.setText("上拉加载更多");
                    break;
                //状态：正在加载中
                case LOADING_MORE:
                    ((FooterHolder) holder).mView.setText("正在加载中");
                    break;
                case LOADING_END:
                    ((FooterHolder) holder).mView.setText("已经到底了");
                    break;
            }
        }
    }

    /**
     * 设置子表单的layout,使用CommonAdapter必须的步骤
     */
    public void setItemLayout(int itemLayoutId,int horizontalScrollViewId,int itemLayoutShowId,int itemLayoutHideId,int itemLayoutFullWidth) {
        this.itemLayoutId = itemLayoutId;
        this.horizontalScrollViewId = horizontalScrollViewId;
        this.itemLayoutShowId = itemLayoutShowId;
        this.itemLayoutHideId = itemLayoutHideId;
        this.itemLayoutFullWidth = itemLayoutFullWidth;

    }
    /**
     * 设置表单item数据
     */
    public abstract void convert(CommonViewHolder holder, T t, int position);

    //定义点击事件的接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    //定义长点击事件的接口
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }
    //清空左滑列表
    public void clearLeftMenuList() {
        if (list == null)
            list = new ArrayList<>();
        list.clear();
        notifyDataSetChanged();
    }
    //点击事件的监听
    private OnItemClickListener mOnItemClickListener;
    //长按事件的监听
    private OnItemLongClickListener mOnItemLongClickListener;
    //设置点击事件的监听
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
    //设置长按事件的监听
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        mOnItemLongClickListener = onItemLongClickListener;
    }
    //改变当前上拉状态
    public void changeMoreStatus(int status){
        load_more_status= status;
        notifyDataSetChanged();
    }
    //清空并添加Item，供上拉刷新时调用
    public void clearAddItem(List<T> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }
    //末尾添加Item，供上拉加载更多时调用
    public void addMoreItem(List<T> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }
    //返回选择的数据
    public T getItemData(int position) {
        T ret = null;
        if (position >= 0 && position < mList.size()) {
            ret = mList.get(position);
        }
        return ret;
    }
    //设置控件是否分页显示
    public void setMultiPage(boolean multiPage){
        this.multiPage = multiPage;
    }
    //Footer对应Holder
    public class FooterHolder extends RecyclerView.ViewHolder {
        private TextView mView;

        public FooterHolder(View itemView) {
            super(itemView);
            mView = itemView.findViewById(R.id.footer);
        }
    }
}